
import db.MYSQLControl;
import db.MyDataSource;
import driver.PhantomJsDriver;
import driver.chromedriver;
import model.model;
import org.apache.commons.dbutils.QueryRunner;
import org.openqa.selenium.*;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import parse.priceParse;
import url.url;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

public class test2 {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = null;
        DataSource ds = MyDataSource.getDataSource("jdbc:mysql://rm-wz9v9e07mh753tk321o.mysql.rds.aliyuncs.com:3306/car_spider");
        QueryRunner qr = new QueryRunner(ds);
        /*try {

            driver = chromedriver.getChromeDriver();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        driver = PhantomJsDriver.getPhantomJSDriver();
        driver.get("http://car.bitauto.com/tree_chexing/mb_9/");
        driver.manage().window().setSize(new Dimension(1000, 30000));
        driver.manage().window().maximize();
        //System.out.printf("now accesss %s \n", driver.getCurrentUrl());
        Thread.sleep(5000);
        List<WebElement> search_result = driver.findElements(By.xpath("//div[@class='brand-name']/span"));
        List<String> brand = new ArrayList();
        for(WebElement result : search_result){
            brand.add(result.getText());
        }
        String factory;
        String Car_big;
        for (String result : brand){
            (new WebDriverWait( driver, 10)) .until(
                    ExpectedConditions.presenceOfElementLocated(By.xpath(String.format("//span[text()='%s']",result)))
            ).click();
            Thread.sleep(2000);

            List<WebElement> search_result1 = driver.findElements(By.xpath("//*[@id=\"divCsLevel_0\"]/h5"));
            if(search_result1.size()==0) {

                List<WebElement> search_result3 =  driver.findElements(By.xpath("//*[@id=\"divCsLevel_0\"]/div/div"));
                for(int j=1;j<search_result3.size()+1;j++){
                    String current_handles = driver. getWindowHandle();
                    // System.out.println(i+"aaa"+j+"mmm"+driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[1]",i,j))).getText());
                    List<WebElement> search_result4 =  driver.findElements(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div/div[%d]/div/ul/li",j)));
                    WebElement element = driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div/div[%d]/div/div/a/img",  j)));
                    Actions actions = new Actions(driver);
                    actions.moveToElement(element).click().perform();
                    Thread.sleep(1000);
                    Set<String> all_handles = driver.getWindowHandles();
                    Iterator<String> it = all_handles.iterator();
                    factory = result;
                    Car_big = driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div/div[%d]/div/ul/li[1]",j))).getText();
                    while(it.hasNext()){
                        if(current_handles == it.next()) continue;
//跳入新窗口
                        WebDriver window = driver.switchTo().window(it.next());
                        Thread.sleep(1000);

                        (new WebDriverWait( driver, 10)) .until(
                                ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"car_tag\"]/nav/div/div/ul/li[2]/a"))
                        ).click();
                        //driver.findElement(By.xpath("//*[@id=\"car_tag\"]/nav/div/div/ul/li[2]/a")).click();
                        Thread.sleep(1000);
                        //MYSQLControl.inserCarRecall(priceParse.getData(driver.getPageSource()));
                        List<model> data = priceParse.getData(factory,result,Car_big,driver.getPageSource());
                        MYSQLControl.inserCarRecall(data);
                        driver.close();
                    }
                    driver.switchTo().window(current_handles);


                    /*try {
                        qr.update(String.format("INSERT INTO car_price VALUES ('%s','%s','%s','%s');",result,search_result1.get(i-1).getText(),driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[1]",i,j))).getText(),driver.findElement(By.xpath(String.format("//*[@id=\"divCsLevel_0\"]/div[%d]/div[%d]/div/ul/li[%d]",i,j,search_result4.size()))).getText()));
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    */
                }
                Thread.sleep(1000);

            }

        }
        driver.quit();
    }
}


